package demo4;

import java.util.Arrays;
import java.util.Comparator;

/*
class Student implements  Comparable<Student> {
    //这个接口对累的侵入性比较强
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
*/



 /*   @Override
    public int compareTo(Student o) {
      *//**//*  if(this.age>o.age){
            return 1;
        }else if(this.age<o.age){
            return -1;
        }else {
            return 0;
        }*//**//*
        return this.age-o.age;
    }*/
//}
class Student implements Comparable<Student>{
    public String name;
    public int age;

     public Student(String name, int age) {
         this.name = name;
         this.age = age;
     }

     @Override
     public String toString() {
         return "Student{" +
                 "name='" + name + '\'' +
                 ", age=" + age +
                 '}';
     }
     public int compareTo(Student o) {
         return this.age - o.age;
     }
 }

 //比较器 优点 对类的侵入性不强
class AgeComparator implements  Comparator<Student>{


    @Override
    public int compare(Student o1, Student o2) {
        return 0;
    }
}
class NameComparator implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        return 0;
    }
}


public class Test {
    public static void main(String[] args) {
        Student student = new Student("zhangsan",10);
        Student student1 = new Student("lisi",15);
        AgeComparator ageComparator = new AgeComparator();
        System.out.println(ageComparator.compare(student,student1));
        NameComparator nameComparator = new NameComparator();
        System.out.println(nameComparator.compare(student,student1));
    }
    public static void bubbleSort(Comparable[] comparable) {
        for (int i = 0; i < comparable.length - 1; i++) {
            for (int j = 0; j < comparable.length - i - 1; j++) {
                if (comparable[j].compareTo(comparable[j + 1]) > 0) {
                    Comparable tmp = comparable[j];
                    comparable[j] = comparable[j + 1];
                    comparable[j + 1] = tmp;

                }
            }
        }
    }

    public static void main1(String[] args) {
        /*Student[] students = new Student[3];
        students[0] = new Student("zhangsan",10);
        students[1] = new Student("lisi",21);
        students[2] = new Student("wangwu",2);
        //bubbleSort(students);
        System.out.println(Arrays.toString(students));*///冒泡排序 年龄升序输出学生
        /*Student student1 = new Student("张三",10);
        Student student2 = new Student("李四",15);
        System.out.println(student1.compareTo(student2));//-1*/
        System.out.println();


    }
}


